<?php
	class Goods_courses extends CI_Model{

	 	function __construct(){
          	parent::__construct();
      	}

        /**
        * 函数功能介绍：返回课程列表（用于分页显示）
        * 创建人:  Yang Ningshu
        * @param id int
        * @return array
        */
        function get_list_ofpage($limit = 0, $offset = 10,$data = 0){
            $where = "1=1 and v.school_id = 0";
            $order = "v.id desc";

            if(isset($data['cate_id']) && $data['cate_id'] > 0){
                $where .= " AND v.cate_id = '". $data['cate_id']."'";
            }
            
            if(isset($data['kemu']) && $data['kemu'] > 0){
                $where .= " AND v.subject_id = '". $data['kemu']."'";
            }
            
            if(!empty($data['search'])) {
                $where .= " AND v.title like '%".$data['search']."%'";
            }

            $sql = "SELECT v.*,cc.name FROM {$this->db->dbprefix}course as v left join {$this->db->dbprefix}course_cate as cc on(v.cate_id = cc.id and cc.school_id = 0)  WHERE $where ORDER BY ".$order." LIMIT $limit, $offset";
            $result = $this->db->query($sql)->result_array();
            if(isset($data['search']) && $data['search']){
                foreach ($result as $key => $value) {
                    $result[$key]['title'] = str_replace($data['search'],'<font color="red">'.$data['search'].'</font>',$value['title']);
                }
            }
            $sql = "SELECT count(v.id) AS count FROM {$this->db->dbprefix}course as v WHERE $where";
            $count = $this->db->query($sql)->result();
            return array("count" => $count[0]->count, "result" => $result);
        }


        function get_school(){
            $sql = "SELECT school_name,id from {$this->db->dbprefix}o2o_school ";
            $result = $this->db->query($sql)->result_array();
            return $result;
        }


        function get_code($id,$type){
            if($type == 1){
                $sql = "SELECT count(id) as count from {$this->db->dbprefix}o2o_school_system_video_course_codes where use_uid = 0 and course_id=".$id;
            }
            if($type == 2){
                $sql = "SELECT count(id) as count from {$this->db->dbprefix}o2o_school_system_video_course_codes where use_uid > 0 and course_id=".$id;
            }
            $result = $this->db->query($sql)->row_array();
            return $result['count'];
        }


        function get_order_course_records($id = 0){
            $sql = "select * from {$this->db->dbprefix}o2o_school_order_records where id = $id";
            $result = $this->db->query($sql)->row_array();
            return $result;
        }
	}
